jQuery(function($){

	// set height window
	var hight =$(window).height();
	var widthScreen =$(window).width();
	var elementContainer = $(".login-page #container");
	var heightContainer = elementContainer.outerHeight(true);
	if(hight>heightContainer){
		elementContainer.css( "height",hight);
	}else{
		elementContainer.css( "height",heightContainer);
	}
	
	/* popup */
	if(widthScreen<=1024){
		$(".top-dropdown-pst").css({'margin-top':'5px','margin-right':'55px'});
		$('#nav').css({'right':'35px'})
		$(".inline").colorbox({
			inline:true,
			width:"60%",
			onComplete:function(){
				var _thisPopupCenter = $(".tool-popup-content");
				var _thisPopupChild = _thisPopupCenter.find(".ex-bottom-tb-ct");
				var _thisWidthPopupCenter = _thisPopupCenter.width();
				var _thisWidthPopupChild =_thisPopupChild.width();
				var positionChild = (_thisWidthPopupCenter-_thisWidthPopupChild)/2;
				_thisPopupChild.css({"padding-left":positionChild});
			}
		});
		 
	}else{$(".inline").colorbox({
			inline:true,
			width:"55.5%",
			onComplete:function(){
				var _thisPopupCenter = $(".tool-popup-content");
				var _thisPopupChild = _thisPopupCenter.find(".ex-bottom-tb-ct");
				var _thisWidthPopupCenter = _thisPopupCenter.width();
				var _thisWidthPopupChild =_thisPopupChild.width();
				var positionChild = (_thisWidthPopupCenter-_thisWidthPopupChild)/2;
				_thisPopupChild.css({"padding-left":positionChild});
			}	
		});
	}
	// set height window end

	jQuery("#language_login,#top-dropdown-action").msDropDown();
	/*-------- add class focus for input in login form -------*/
	$(".login-from-content .input").each(function() {
		$(this).find("input").blur(function(){
			$('input').removeClass("focus");
		}).click(function(){
			$(this).addClass("focus");
		});
		
	});
	
	/*----- validate form login ----*/
	jQuery(".remember input[type=checkbox],.tool-popup-content table input[type=checkbox],.table-content input[type=checkbox],.list-check-map input[type=checkbox]").uniform();
	$(".main-form-content select").uniform();
	$(".form-mapleft-menu select").uniform();
	$(".add-form-top input[type=file]").uniform({fileButtonHtml:"Import File",fileDefaultHtml:"No file selected"});
	//$.uniform.defaults.fileBtnText = "Pick a file";
	 $("#login-form").validate({
		rules: {
			username: {
				required: true
			},
			password: {
				required: true
			}
		},
		messages: {
			username: "",
			password: {
				required: ""
			}
		},
		submitHandler: function(form) {
			form.submit();
		}
	});
	
	/*--------- current menu login form-------------*/
	$(".index-menu-content li").each(function() {
		var _parent = $(this);
		var showChild = _parent.find('.tooltip_description');

		_parent.find("a.bg-iterm").hover(function () {
			$(this).parent().addClass("current");
		},function () {
			$(this).parent().removeClass("current");
		});
		_parent.find('.bg-iterm').live( {mouseover : function() {
				var timeout = setTimeout(function() {
					showChild.show();
				});
			}, mouseout: function () {
				timeout = setTimeout(function() {
					showChild.hide();    
				}, 300);
			}
		});
	});

	/*------------- layout resize----------------*/
	
	var heightHeader = $('.plan-child #header-container').outerHeight(true);
	var heightfooter = $('.plan-child #footer-container').outerHeight(true);
	var heightContainerChild = hight -(heightHeader+heightfooter);
	$('.plan-child #content-container').height(heightContainerChild);

	$(function($){
		var scntDiv = $('#indicator-form');
		var _selectauto = scntDiv.find('.select-auto');
		var i = _selectauto.size() + 1;
		
		_selectauto.find('.remove-itemr').live('click', function() { 
			if( i > 1 ) {
					$(this).parents('.select-auto').remove();
					i--;
			}
			return false;
		});
		
		var container = $('.hasleft-layout');
		var center = $('.hasleft-layout .center');
		var west = $('.hasleft-layout .west');
		
		var maxwidth =container.width()-805;

		//fix heigh indicator
		var heightCenter = center.height();
		var heightTile = center.find(".h2-title-2").outerHeight(true);
		var childCenter = center.find(".content-detail");
		var _this_showdowLine = center.find(".shasdow-line");
		var indicatorContent =childCenter.find(".indicator-content");
		var childFixBlock = childCenter.find(".indicator-content .indicator-block");
		var childFixBlockL = childCenter.find(".indicator-content .left-block-content");
		var childFixBlockR = childCenter.find(".indicator-content .right-block-content");
		var heightFixBlock = childFixBlock.height();
		var heightFixBlockL = childFixBlockL.height();
		var heightFixBlockR = childFixBlockR.height();
		var heightChildCenter = childCenter.height();
	
		
		/* center button */
		var _formMainForm = $(".main-form-content");
		var _buttonFormMainForm = _formMainForm.find(".group-bt");
		var _inputCenter = _formMainForm.find(".input-center");
		var _indicatorEditor = $(".editor-content");
		var _buttonIndicatorEditor =_indicatorEditor.find(".group-bt");
		var _planadmineditorForm = $(".planadmin_editor");
		var _inputEditorCenter =_planadmineditorForm.find(".group-bt");
		var margin_Button_form_center =( _formMainForm.width()-_buttonFormMainForm.outerWidth(true))/2;
		var paddingInputCenter = (_formMainForm.width()-_inputCenter.outerWidth(true))/2;
		var paddingInputEditorCenter = (_planadmineditorForm.width()-_inputEditorCenter.outerWidth(true))/2;
		_buttonFormMainForm.css({"left":margin_Button_form_center});
		_inputCenter.css({"padding-left":paddingInputCenter});
		_inputEditorCenter.css({"padding-left":paddingInputEditorCenter});


		var _paddingButtonIndicatorEdit = (_indicatorEditor.width()-_buttonIndicatorEditor.outerWidth(true))/2;
		_buttonIndicatorEditor.css({"padding-left":_paddingButtonIndicatorEdit});

		if(heightCenter>heightChildCenter){
			heightFixBlock = heightCenter-(heightTile+103);
			childFixBlockR.height(heightFixBlock);
			childFixBlockL.height(heightFixBlock);
		}else{
			_this_showdowLine.height(childCenter.outerHeight(true)+heightTile);
			if(heightFixBlockL>heightFixBlockR){
				childFixBlockR.height(heightFixBlockL);
				childFixBlockL.height(heightFixBlockL);
			}else{
				childFixBlockL.height(heightFixBlockR);
			}
		}
		
		//fix width input report form
		var _thisFormRepost = $(".report-form fieldset .input");
		var _thisFormRepostLabel = _thisFormRepost.find("label");
		var _thisFormRepostSpan = _thisFormRepost.find("span");
		var _thisFormRepostInput = _thisFormRepost.find("input");
		var _thisFormRepostTextarea = _thisFormRepost.find("textarea");		
		var _thisToggleWestSelect =$('#toggle-west');

		//fix heigh indicator .end
		if(widthScreen > 1024){
			function relayout() {
				container.layout({resize: false});
				var widthIndicatorContent = indicatorContent.width();
				var widthChildFixBlock = parseInt((widthIndicatorContent-64)/2);
				childFixBlock.width(widthChildFixBlock);

				var _widthFixInput = _thisFormRepost.width()-_thisFormRepostLabel.outerWidth(true)-_thisFormRepostSpan.outerWidth(true)-(_thisFormRepostInput.outerWidth(true)-_thisFormRepostInput.width());
				_thisFormRepostTextarea.width(_widthFixInput);
				_thisFormRepostInput.width(_widthFixInput);
			}
			relayout();

			$(window).resize(relayout);
			$('.south').resizable({
				handles: 'n',
				stop: relayout
			});
			$('.west').resizable({
				handles: 'e',
				stop: relayout,
				minWidth:299,
				maxWidth: maxwidth
			});
			_thisToggleWestSelect.click(function () {
				$(this).toggleClass("toggle-west-open");
				$('.west').animate({width: ['toggle', 'swing']}, {duration: 500, complete: relayout, step: relayout});
			});
		}else{
			_thisToggleWestSelect.click(function () {
				$(this).toggleClass("toggle-west-open");
				$('.west').animate({width: ['toggle', 'swing']}, {duration: 500});
			});

			//$("#ui-datepicker-div").css({"position":"absolute","left":"113px","top":"156px"});
			var widthIndicatorContent = indicatorContent.width();
			var widthChildFixBlock = parseInt((widthIndicatorContent-64)/2);
			childFixBlock.width(widthChildFixBlock);

			var _widthFixInput = _thisFormRepost.width()-_thisFormRepostLabel.outerWidth(true)-_thisFormRepostSpan.outerWidth(true)-(_thisFormRepostInput.outerWidth(true)-_thisFormRepostInput.width());
			_thisFormRepostTextarea.width(_widthFixInput);
			_thisFormRepostInput.width(_widthFixInput);
		}
	});

	/* accordion menu tree*/
	var accordion = $(".accordion");
	accordion.find("h3").each(function (index, elem) {
		if ($(this).hasClass("ui-state-active")) {
			accordion.accordion({ fillSpace: true,active:index});
		}else{accordion.accordion({ fillSpace: true,active:false});}
	});
	$(".accordion-report").accordion({autoHeight: false});
	/* tree menu*/
	$(".browser-tree").treeview();
	$('.hover-option-tooltip').tooltipster({ position: 'left'});
	$('.hover-tooltip-map').tooltipster({ position: 'bottom'});
	
	// add and remove
	$('.close').live('click',function(e){
		$(this).parent().hide();
		e.stopPropagation();
	});
	$('.hastoogle').live('click',function(e){
		$('.close').trigger('click');
		$(this).find(".show-hide").show();
		e.stopPropagation();
	});
	$(document).click(function(e){
		$('.close').trigger('click');
		e.stopPropagation();
	});
	
	//calendar
	$( "#create_date" ).datepicker({dateFormat : 'dd/mm/yy'});
	$(function (){		
		var txtStartDate1 = $('#fromdate');
		var txtEndDate1 = $('#todate');
		var txtStartDate2 = $('#fromdate2');
		var txtEndDate2 = $('#todate2');

		txtStartDate1.datepicker({
			numberOfMonths: 1,
			showOn          :'button',
			buttonImage     :'images/calendar-icon.png',
			buttonImageOnly :true,
			dateFormat : 'mm/yy',
			onSelect: function(selected) {
				txtEndDate1.datepicker("option","minDate", selected)
			}
		});
		txtEndDate1.datepicker({ 
			numberOfMonths: 1,
			showOn          :'button',
			buttonImage     :'images/calendar-icon.png',
			buttonImageOnly :true,
			dateFormat : 'mm/yy',
			onSelect: function(selected) {
				txtStartDate1.datepicker("option","maxDate", selected)
			}
		});
		txtStartDate2.datepicker({
			numberOfMonths: 1,
			showOn          :'button',
			buttonImage     :'images/calendar-icon.png',
			buttonImageOnly :true,
			dateFormat : 'mm/yy',
			onSelect: function(selected) {
				txtEndDate2.datepicker("option","minDate", selected)
			}
		});
		txtEndDate2.datepicker({ 
			numberOfMonths: 1,
			showOn          :'button',
			buttonImage     :'images/calendar-icon.png',
			buttonImageOnly :true,
			dateFormat : 'mm/yy',
			onSelect: function(selected) {
				txtStartDate2.datepicker("option","maxDate", selected)
			}
		});
	});
	/* tabs */
	$('.tabs').each(function(){
		$(this).find('.section').hide();

		var current = $(this).find('.item').children('.current');
		if (current.length == 0){
			$(this).find('.item').children(':first-child').addClass('current');
			$($(this).find('.item').children(':first-child').find('a').attr('href')).show();
		}

		$(this).find('.item').find('a').click(function(){
			var current = $(this).parent().hasClass('current');
			if (current == false){
				$(this).parent()
					.addClass('current')
					.siblings().each(function(){
						$(this).removeClass('current');
						$($(this).find('a').attr('href')).hide();
					});
				$($(this).attr('href')).fadeIn();
			}
			return false;
		});
	});	
	/* drap map  */
	var $thisBlockMap = $('.map-block');
	var _thisDrap_left = $thisBlockMap.find('.drag-left-map');
	$('.drag-left-map,.drag-right-map').drag(function( ev, dd ){
		dd.limit = $thisBlockMap.offset();
		dd.limit.bottom = $thisBlockMap.outerHeight(true) - $( this ).outerHeight(true);
		dd.limit.right = $thisBlockMap.outerWidth(true) - $( this ).outerWidth(true);
		$( this ).css({
			top: Math.min( dd.limit.bottom, Math.max( 0, dd.offsetY ) ),
			left: Math.min( dd.limit.right, Math.max( 0, dd.offsetX ) )
		});
	},{ relative:true },{ handle:".drag-handle" }); 
	
});
